﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Shop
{
    class DataHelper
    {
        public MySqlConnection connection;

        //construtor
        public DataHelper()
        {
            String connectionInfo = "server=athena01.fhict.local;" +
                "database=dbi301970;" + "user id=dbi301970;" + "password=RtNR7HGBTZ;" + "connect timeout=30;";
            connection = new MySqlConnection(connectionInfo);
        }

        //method:Register a new customer
        public int AddNewCust(string cust_id,string first_name, string last_name, string email, string bank_account, int mobile,
            string birth_date, string gender, string country, string city, string bracelet)
        {
            string sql = "INSERT INTO product_info VALUES('" +cust_id+"','"+ first_name + "','" + last_name + "'," + email + "," +
                bank_account + "," + mobile + "," + birth_date + "," + gender + "," + country + "," + city+","+bracelet +")";
            MySqlCommand command = new MySqlCommand(sql, connection);

            try
            {
                connection.Open();
                int nrOfRecordsChanged = command.ExecuteNonQuery();
                return nrOfRecordsChanged;
            }
            catch
            {
                return -1;
            }
            finally
            {
                connection.Close();
            }

        }

        // method: compare the Cust ID on the entrance
        public CustInfo FindCustID(string cust_id)
        {
            string sql = "SELECT * FROM customer WHERE Cust Id ='" + cust_id + "'";
            MySqlCommand command = new MySqlCommand(sql, connection);


            CustInfo CustomerInfo = new CustInfo();

            try
            {
                connection.Open();

                MySqlDataReader reader = command.ExecuteReader();
                string custId, firstName, lastName, Email, bank_account, birth_date, gender, country, city;
                int mobile;


                while (reader.Read())
                {
                    custId = Convert.ToString(reader["Cust Id"]);
                    firstName = Convert.ToString(reader["First Name"]);
                    lastName = Convert.ToString(reader["Last Name"]);
                    Email = Convert.ToString(reader["Email"]);
                    bank_account = Convert.ToString(reader["Bank Account"]);
                    mobile = Convert.ToInt16(reader["Mobile"]);
                    birth_date = Convert.ToString(reader["Birth Date"]);
                    gender = Convert.ToString(reader["Gender"]);
                    country = Convert.ToString(reader["Country"]);
                    city = Convert.ToString(reader["City"]);
                    CustomerInfo= new CustInfo(custId, firstName, lastName, Email, bank_account, mobile, birth_date, gender, country, city);
                    
                }

                
            }
           catch
            { MessageBox.Show("Cannot find the customer!");
            
           }
            finally
            {
                connection.Close();

            }
            return CustomerInfo;
        }
    }
}
